package reliability.ui;

import reliability.model.GraphModel;
import reliability.model.MinimalPathsAndSectionsMethodResults;
import reliability.model.StatisticalModelingMethodResults;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InfoPanel
    extends JPanel
    implements ActionListener
{
    public static final Font DEFAULT_FONT = new Font("TimesRoman", Font.TRUETYPE_FONT, 12);

    private GraphModel model;
    private JButton computeButton;
    private ResultsPanel resultsPanel;
    ProgressDialog progressDialog;

    public InfoPanel(GraphModel model)
    {
        super();
        this.model = model;

        setLayout(new FlowLayout());
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        addCurrentElementPanel();
        addDefaultReliabilityPanel();
        addResultsPanel();
        addComputeButton();
    }


    public void actionPerformed(ActionEvent actionEvent)
    {
        if (!model.getEdges().isEmpty())
        {
            progressDialog = new ProgressDialog((JFrame) SwingUtilities.getRoot(this), "Расчет надежности", model);
            progressDialog.setVisible(true);
            if (!progressDialog.isCancelled())
            {
                showResults();
            }
        }
        else
        {
            JOptionPane.showMessageDialog(this,
                    "Между узлами сети должна присутствовать хотя бы одна линия связи",
                    "Ошибка в графе",
                    JOptionPane.ERROR_MESSAGE
            );
        }
    }

    private void showResults()
    {
        StatisticalModelingMethodResults statisticalModelingMethodResults = progressDialog.getStatisticalModelingMethodResults();
        MinimalPathsAndSectionsMethodResults minimalPathsAndSectionsMethodResults = progressDialog.getMinimalPathsAndSectionsMethodResults();
        resultsPanel.showResults(statisticalModelingMethodResults, minimalPathsAndSectionsMethodResults);
        if (statisticalModelingMethodResults.networkReliability == 0)
        {
            JOptionPane.showMessageDialog(this,
                    "Получена нулевая вероятность безотказной работы.\n" +
                            "Увеличьте количество испытаний, либо повторите расчет.",
                    "Предупреждение",
                    JOptionPane.WARNING_MESSAGE
            );
        }
    }

    private void addCurrentElementPanel()
    {
        CurrentElementPanel currentElementPanel = new CurrentElementPanel(model);
        add(currentElementPanel);
    }

    private void addDefaultReliabilityPanel()
    {
        DefaultReliabilityPanel defaultReliabilityPanel = new DefaultReliabilityPanel(model);
        add(defaultReliabilityPanel);
    }

    private void addResultsPanel()
    {
        resultsPanel = new ResultsPanel(model);
        add(resultsPanel);
    }

    private void addComputeButton()
    {
        computeButton = new JButton("Рассчитать");
        computeButton.addActionListener(this);
        add(computeButton);
    }
}
